package com.lv.service.adapter

import android.graphics.Color
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lv.common.base.DataBindBaseViewHolder
import com.lv.common.imp.OnPositionTListener
import com.lv.common.ktx.LoadSrc
import com.lv.common.utils.MoneyUtil
import com.lv.service.R
import com.lv.service.bean.CardItemBean
import com.lv.service.databinding.ItemSelectedCardStyleBinding
import com.lv.service.room.card.CardColorBean

/**
 * 作者： ygx
 * 创建日期：2022/6/24 8:10 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 单选弹窗 选择器
 */
class CardStyleSelectedAdapter(
    val mStyles: MutableList<CardColorBean>?,
    private val datas: MutableList<CardItemBean>?
) :
    BaseQuickAdapter<CardItemBean, DataBindBaseViewHolder>
        (R.layout.item_selected_card_style, datas) {
    private val mOneUrl =
        "https://suyiyong.oss-cn-hangzhou.aliyuncs.com/iOS/projectPic/syy/mine_myStore_card2.png"
    private val mTwoUrl =
        "https://suyiyong.oss-cn-hangzhou.aliyuncs.com/1231/huis_WH_960_302.png"

    override fun convert(
        holder: DataBindBaseViewHolder,
        item: CardItemBean,
        payloads: List<Any>
    ) {
        super.convert(holder, item, payloads)
        val mBinding: ItemSelectedCardStyleBinding =
            holder.getBind() as ItemSelectedCardStyleBinding
//        mBinding.itemRoot = item
//        //防止列表闪烁
        mBinding.executePendingBindings()
        mBinding.ivSelected.LoadSrc(if (item.isSelectd) com.lv.common.R.drawable.ic_selected else com.lv.common.R.drawable.ic_unselected)

    }

    override fun convert(holder: DataBindBaseViewHolder, item: CardItemBean) {
        val mBinding: ItemSelectedCardStyleBinding =
            holder.getBind() as ItemSelectedCardStyleBinding
//        mBinding.itemRoot = item
//        //防止列表闪烁
        mBinding.executePendingBindings()
        val layoutPosition = holder.layoutPosition
//        val enterBeans = mStyles?.filter { color -> item.styleid == color.id }
        val value = layoutPosition % 2
        when (value) {
            0 -> mBinding.ivBanner.LoadSrc(mOneUrl)
            else -> mBinding.ivBanner.LoadSrc(mTwoUrl)
        }
//        if (!enterBeans.isNullOrEmpty()) {
//            val cardpage = enterBeans[0].newcardpage
//            mBinding.ivBanner.LoadSrc(cardpage?.image)
//            val colors = intArrayOf(
//                YgxUtilsKt.parseColor(cardpage!!.cardfontcolor!![0]),
//                YgxUtilsKt.parseColor(cardpage.cardfontcolor!![1]),
//            )
//
//            mBinding.tvCardText.gradientColors = colors
//            mBinding.tvDec.setTextColor(YgxUtilsKt.parseColor(cardpage.commonfontcolor!![0]))
//            mBinding.tvDays.setTextColor(YgxUtilsKt.parseColor(cardpage.cardfontcolor!![1]))
//        }
        mBinding.tvCardText.text = item.name + "  "
        val periods = item.periods ?: 0
        mBinding.tvGive.text = "赠${periods}次期刊"
//        val year = item.year ?: 1
//        val showDays = year * 365
//        mBinding.tvDays.text = "${showDays}天"
        val price = item.price ?: 0
        val priceStr = MoneyUtil.moneydiv("$price", "100")
        val term = item.term
        mBinding.tvUnit.text = "/$term"
        mBinding.tvPrice.text = "¥$priceStr"
        if (price == 0) {
//            mBinding.tvPrice.visibility = View.GONE
//            mBinding.tvUnit.visibility = View.GONE
            mBinding.tvPrice.setTextColor(Color.parseColor("#ffffff"))
            mBinding.tvUnit.setTextColor(Color.parseColor("#ffffff"))
        } else {
//            mBinding.tvPrice.visibility = View.VISIBLE
//            mBinding.tvUnit.visibility = View.VISIBLE
            mBinding.tvPrice.setTextColor(Color.parseColor("#0A1623"))
            mBinding.tvUnit.setTextColor(Color.parseColor("#0A1623"))
        }
        mBinding.tvGive.visibility = if (periods == 0) View.GONE else View.VISIBLE
        mBinding.ivSelected.LoadSrc(if (item.isSelectd) com.lv.common.R.drawable.ic_selected else com.lv.common.R.drawable.ic_unselected)
        mBinding.clParent.setOnClickListener {
            onViewItemClickListener?.onClick(layoutPosition, item)
        }
    }

    var onViewItemClickListener: OnPositionTListener<CardItemBean>? = null

}